package com.example.boxuegu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boxuegu.DengZhu.MyDenglu;

public class modify_xml extends AppCompatActivity {

    private TextView tv_main_title;
    private TextView tv_back;
    private EditText old_psw;
    private EditText new_psw;
    private EditText new_psw_again;
    private Button but_save;
    private String old_psw1,new_psw1,new_psw_again1;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_xml);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

    private void init() {
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("修改密码");
        tv_back = (TextView) findViewById(R.id.tv_back);
        old_psw = (EditText) findViewById(R.id.old_psw);
        new_psw = (EditText) findViewById(R.id.new_psw);
        new_psw_again = (EditText) findViewById(R.id.new_psw_again);
        but_save = (Button) findViewById(R.id.but_save);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modify_xml.this.finish();
            }
        });
        /*
        * 保存按钮点击事件*/
        but_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getEditString();
                if(TextUtils.isEmpty(old_psw1)){
                    Toast.makeText(modify_xml.this,"请输入原始密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if(!MD5.md5(old_psw1).equals(readPsw())){
                    Toast.makeText(modify_xml.this,"输入的密码和原始密码不一致",Toast.LENGTH_SHORT).show();
                    return;
                }else if(MD5.md5(new_psw1).equals(readPsw())){
                    Toast.makeText(modify_xml.this,"新密码不能与原始密码一样",Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(new_psw1)){
                    Toast.makeText(modify_xml.this,"请输入新密码",Toast.LENGTH_SHORT).show();
                    return;

                }else if(TextUtils.isEmpty(new_psw_again1)){
                    Toast.makeText(modify_xml.this,"请再次输入新密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if(!new_psw1.equals(new_psw_again1)){
                    Toast.makeText(modify_xml.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText(modify_xml.this,"新密码设置成功",Toast.LENGTH_SHORT).show();
                    modifyPsw(new_psw1);
                    Intent intent = new Intent(modify_xml.this, MyDenglu.class);
                    startActivity(intent);
                    setting.instance.finish();//关闭设置界面
                    modify_xml.this.finish();//关闭本界面
                }

            }
        });
    }
    /*
    * 修改成功后保存新密码至sharedpreference中*/
    private void modifyPsw(String newPsw) {
        String md5=MD5.md5(newPsw);//把密码用MD5加密
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor edit = loginInfo.edit();//获取编辑器
        edit.putString(username,md5);
        edit.commit();
    }
    /*
    * 从sharedPreferences中读取原始密码*/

    private String readPsw() {
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", MODE_PRIVATE);
        String string = loginInfo.getString(username, "");
        return string;

    }

    /*
    * 获取控件上的字符串*/
    private void getEditString() {
        old_psw1 = old_psw.getText().toString().trim();
        new_psw1 = new_psw.getText().toString().trim();
        new_psw_again1 = new_psw_again.getText().toString().trim();

    }
}